﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WickedSick.ClientComponents.Music;

namespace SDB
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        private MediaBI _MediaBI;
        private bool _LoggedIn = false;
        private Guid _LoginGuid;

        public Login(MediaBI mbi)
        {
            InitializeComponent();
            
            _MediaBI = mbi;
            _MediaBI.UserAuthenticatedComplete += new UserAuthenticatedCompleteDelegate(_MediaBI_UserAuthenticatedComplete);

            chkImpersonate.Checked += new RoutedEventHandler(chkImpersonate_Checked);
            chkImpersonate.Unchecked += new RoutedEventHandler(chkImpersonate_Unchecked);
            btnLogin.Click += new RoutedEventHandler(btnLogin_Click);

            this.Closing += new System.ComponentModel.CancelEventHandler(Login_Closing);

            _LoggedIn = false;
        }

        private void Login_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!_LoggedIn)
            {
                e.Cancel = true;
            }
        }

        private void _MediaBI_UserAuthenticatedComplete(object sender, Guid id)
        {
            if (id == Guid.Empty)
            {
                InputPanel.IsEnabled = true;
                MessageBox.Show("Invalid username or password.", "Failed login", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                //TODO: Associate windows login?
                //TODO: Save username/password cookie

                _LoggedIn = true;
                _LoginGuid = id;
                InputPanel.IsEnabled = true;
                this.Hide();
            }
            DisableWorkingMarquee();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (txtUsername.Text.Length < 1)
            {
                MessageBox.Show(this, "You must specify a username.", "Invalid username", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else if (txtPassword.Password.Length < 1)
            {
                MessageBox.Show(this, "You must specify a password.", "Invalid password", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            EnableWorkingMarquee();
            InputPanel.IsEnabled = false;
            _MediaBI.AuthenticateUser(txtUsername.Text, txtPassword.Password);
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            GlassHelper.ExtendGlassFrame(this, new Thickness(LeftGrid.ActualWidth, 10, 10, 35));
            txtUsername.Focus();
        }

        private void chkImpersonate_Checked(object sender, RoutedEventArgs e)
        {
            chkRemember.IsChecked = true;
            chkRemember.IsEnabled = false;
        }

        private void chkImpersonate_Unchecked(object sender, RoutedEventArgs e)
        {
            chkRemember.IsChecked = false;
            chkRemember.IsEnabled = true;
        }

        private void EnableWorkingMarquee()
        {
            pbWorking.Visibility = Visibility.Visible;
        }

        private void DisableWorkingMarquee()
        {
            pbWorking.Visibility = Visibility.Hidden;
        }

        public Guid LoginGuid
        {
            get { return _LoginGuid; }
        }
    }
}
